//
//  UserAccountModel.swift
//  CSYMicroBlockSina
//
//  Created by 姚彦兆 on 15/11/9.
//  Copyright © 2015年 姚彦兆. All rights reserved.
//

import UIKit

/* [拦截到CODE_URL]http://m.neihanshequ.com/
{
    "access_token" = "2.00fu7oCG0tFG7h324e19ff98oUkqTD";
    "expires_in" = 157679999;
    "remind_in" = 157679999;
    uid = 5538369521;
}*/

//拦截到了用户信息之后获得相应的权限进行处理
class UserAccountModel: NSObject , NSCoding {

    
    
    //用户的权限Token模型值
    var access_token : String?
    
    //用户的过期时间
    var expires_in : NSTimeInterval = 0 {
    
        didSet{
        
            expires_date = NSDate(timeIntervalSinceNow: expires_in)
        
        }
    
    }
    
//    //沙盒路径
//    var path : String {
//    
//        return (NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("account.plist")
//    
//    }
    
    //用户的具体过期日期
    var expires_date : NSDate?
    
    //用户的授权ID
    var uid : String?
    
    //用户的头像地址
    var avatar_hd : String?
    
    //用户的昵称
    var name : String?
    
    init(userInfo dict:[String : AnyObject]) {
     
        super.init()
        
        setValuesForKeysWithDictionary(dict)
    
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) { /*实现不完全字典赋值*/ }
    
    override var description : String {
    
        let keys = ["access_token" , "expires_in" , "expires_date" , "uid" , "avatar_hd" , "name"]
    
        return dictionaryWithValuesForKeys(keys).description
        
    }

    
    //对外方法
    
    //--->归档
    func saveAccount ()  {
    
        let path = (NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("account.plist")
        NSKeyedArchiver.archiveRootObject(self, toFile: path)
        //print(path)
        
//        let path = (NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("account.plist")
//        NSKeyedArchiver.archiveRootObject(self, toFile: path)
    
    }
    //--->解档
    class func getUserInfoFromDocumentDirectoryPlist() -> UserAccountModel? {
    
        let path = (NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("account.plist")
    
        //print("沙盒路径\(path)")
        
        if let userAccount = NSKeyedUnarchiver.unarchiveObjectWithFile(path) as? UserAccountModel {
            
            //print("解档数据\(userAccount)")
            
            if userAccount.expires_date?.compare(NSDate()) == NSComparisonResult.OrderedDescending {
            
                //print("解档操作\n\(userAccount)\n\n")
                
            return userAccount
            
            }
        
        }
        
        return nil
        
    }
    
    
    //归档
    func encodeWithCoder(aCoder: NSCoder) {
        
        aCoder.encodeObject(access_token, forKey: "access_token")
        
        aCoder.encodeDouble(expires_in, forKey: "expires_in")
        
        aCoder.encodeObject(expires_date, forKey: "expires_date")
        
        aCoder.encodeObject(uid, forKey: "uid")
        
        aCoder.encodeObject(avatar_hd, forKey: "avatar_hd")
        
        aCoder.encodeObject(name, forKey: "name")
        
    }
    
    //解档
    required init?(coder aDecoder: NSCoder) {
        
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        
        expires_in = aDecoder.decodeDoubleForKey("expires_in")
        
        expires_date = aDecoder.decodeObjectForKey("expires_date") as? NSDate
        
        uid = aDecoder.decodeObjectForKey("uid") as? String;
        
        avatar_hd = aDecoder.decodeObjectForKey("avatar_hd") as? String
        
        name = aDecoder.decodeObjectForKey("name") as? String
        
    }
    
    
}

// MARK: - 进行归档解档操作
extension UserAccountModel {

    
    

}






























